// components/drawsheet/index.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    drawsheet: {
      type: Object,
      value: undefined,
      observer: function (newVal, oldVal) {

        this._drawsheetpropertyChange(newVal, oldVal);

      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'

    },
    matchinfo: {
      type: Object,
      value: undefined,
      observer: function (newVal, oldVal) {

        this._matchinfopropertyChange(newVal, oldVal);

      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'


    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    staticUrl: app.globalData.staticUrl,
    currentTab: 0,
    roundno:1,
    currentpageheight: 0,
    top: 999,
    istopnav: false,
    inittop: 0,
    curClass: '',
    headerHeight:undefined,
    contenttop:0,
  },

  /**
   * 组件的方法列表
   */
  methods: {


    //打开playerscorecard 页面 
    _openplayerCard(e) {
      this.setData({
        selectplayerid: e.currentTarget.dataset.id,
        teamorplayer: 0
      });
      var playername = e.currentTarget.dataset.playername;
      let id = (this.data.teamorplayer == 0) ? this.data.selectplayerid : this.data.selectteamid;
      // 微信小程序中是通过triggerEvent来给父组件传递信息的
      this.triggerEvent('_openplayerCard', {
        playerorteam: this.data.teamorplayer,
        selectid: id,
        playername: playername,
        matchinfo: JSON.stringify(this.data.matchinfo)
      }) // 将参数的形式传递给父组件

    },

    fillheight() {
     
      var that = this;
      var obj = this.createSelectorQuery().select("#vdrawsheetcontent" + that.data.currentTab);

      obj.boundingClientRect(function (rect) {
        if (rect == null || rect.height == null) {
          return;
        }
        var fullheight = wx.getSystemInfoSync().windowHeight;
        var windowswidth = wx.getSystemInfoSync().windowWidth;
        var pixelRatio = windowswidth/750 ;// wx.getSystemInfoSync().pixelRatio;
        console.log("pixelRatio");
        console.log(pixelRatio);
        if(rect.height> fullheight){
          that.setData({
            currentpageheight: fullheight /pixelRatio  // 节点的高度
          });
          console.log("fullheight");
          console.log(fullheight);
        }
        else
        {

        that.setData({
          currentpageheight: rect.height / pixelRatio +60  // 节点的高度
        });
        
        console.log(rect.height);
        console.log(that.data.currentpageheight);
      }
        
      }).exec();

    },
    //  tab切换逻辑
    swichNav: function (e) {
      var that = this;
      if (that.data.currentTab === e.target.dataset.current) {

        return false;
      } else {
        that.setData({
          currentTab: e.target.dataset.current
        })

        that.fillheight();
      }
    },

    bindPickerChangeround:function(e){
      var roundid = e.detail.value;
      var roundno = this.data.matchinfo.rounds[roundid].roundno;
      console.log("bindPickerChangeround");
      this.setData({roundno:roundno});
      console.log(roundno);
      this.triggerEvent('bindPickerChangeround', {
       roundno:roundno
      }) // 将参数的形式传递给父组件

    },

    bindChange: function (e) {
      var that = this;
      that.setData({
        currentTab: e.detail.current
      });
      that.fillheight();
    },

    _matchinfopropertyChange(newVal, oldVal) {
      var that = this;
      var jsonmatchinfoobj = newVal;
      if (jsonmatchinfoobj.currentround == 0) {
        that.setData({
          currentTab: 0,
          roundno:1,
        });
      } else {
        that.setData({
          currentTab: jsonmatchinfoobj.currentround - 1,
          roundno:jsonmatchinfoobj.currentround
        });
        console.log("_matchinfopropertyChange");
        console.log(newVal);
      }

      this.fillheight();
    },

    _drawsheetpropertyChange(newVal, oldVal) {

      this.fillheight();
    },

    scrollTop(e){
      let that = this;
      if(e.detail.scrollTop<=0 && that.data.istopnav){
        that.setData({
          istopnav: false
          })
      }
      else{
      
      }

    },
    //控制回到顶部按钮的显示与消失
    _scrollTopFun(e) {
      let that = this;
     
     // if (e.scrollTop > that.data.headerHeight && !that.data.istopnav) {
        // 当页面顶端距离大于一定高度时
    ////    that.setData({
    //      istopnav: true
   //     })
  //      console.log(that.data.istopnav);
    //    console.log(e.scrollTop);
 //    
//     } else if (e.scrollTop <= that.data.headerHeight && that.data.istopnav) {
//        that.setData({
//          istopnav: false
//        })
 //       console.log("empty");
//        console.log(e.scrollTop);
//      }




  
     var objtitle = that.createSelectorQuery();
     objtitle.select("#vdrawsheet").boundingClientRect(function (rect) {
       that.setData({
         top: rect.top // 节点的高度
       })
       if(that.data.top<=0){
        that.setData({
            istopnav: true
             })
         
       }
        else{
          that.setData({
           istopnav: false
            })
        }
     }).exec();

    }




    }
  
})